package friendManager;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import Bean.SqlBean;
import loginRegister.LoginBean;

/**
 *
 * @author 刘杉
 */
public class LookFriendServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String userName="";
        HttpSession session=request.getSession();
        LoginBean nn=(LoginBean)session.getAttribute("login");
        if(nn==null){
            response.sendRedirect("http://localhost:8080/login.jsp");
        }else{
            userName=nn.getUserName();
            System.out.println("hello");
            String sql="select * from friends where userName='"+userName+"'";
            try {
                ResultSet rs=null;
                SqlBean sb=new SqlBean();
                sb.connectSQL();
                rs=sb.executeQuery(sql);
                ArrayList friendslist=null;
                if((ArrayList)session.getAttribute("friendslist")==null){
                    friendslist=new ArrayList();
                    while(rs.next()){
                        FriendBean ff=new FriendBean();
                        ff.setName(rs.getString("name"));
                        ff.setPhone(rs.getString("phone"));
                        ff.setEmail(rs.getString("email"));
                        ff.setWorkPlace(rs.getString("workPlace"));
                        ff.setPlace(rs.getString("place"));
                        ff.setQQ(rs.getString("QQ"));
                        friendslist.add(ff);
                    }
                    sb.closeSQL();
                    session.setAttribute("friendslist", friendslist);
                }
                response.sendRedirect("http://localhost:8080/friendManager/lookFriend.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        doGet(request, response);
    }

}